<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Catalog_ctrl extends CI_Controller {
	public $pageName="catalog";
	public function __construct(){
		parent::__construct();//创造型函数
		$this->load->model('Catalog_model');
		if (!$this->session->userdata('is_login')) {
			redirect('admin/auth/login');//父容器元素。子集集成
		}
	}

	public function index(){
		$names =$this->Catalog_model->getAll();
		$data = array(
			'names' =>$names                      //拿到的数据打包进$data
		);
		$this->load->view('admin/Catalog_view',$data);
	}

	public function add_one(){
        $this->load->library('form_validation');               //加载该项才能用
        $this->form_validation->set_rules('name', '标题', 'required');          //限制条件，必
        	$catalog=new stdClass();                  //创建一个空的对象，进行欺骗
        	$catalog->name="";
        if ($this->form_validation->run() == FALSE){
        	$this->load->model('Catalog_model');
        	$data=array(
        		'title'=>"添加",
        		'name'=>$catalog
        	);
        	$this->load->view('admin/Catalog_form',$data);
        }else{
            $data=$this->input->post();           //拿到POST过来的数据，塞进数组里面。
            $this->load->model('Catalog_model');
            $this->Catalog_model->add_one($data);
            redirect('admin/Catalog_ctrl/index');
        }
	}

	public function edit_one($id){
        $this->load->library('form_validation');            //加载该项才能用
        $this->form_validation->set_rules('name', '标题', 'required');                //限制条件，必填项
            $this->load->model('Catalog_model');
        if ($this->form_validation->run() == FALSE){
        	$this->load->model('Catalog_model');
        	$data=array(
        		'name'=>$this->Catalog_model->getOne($id),
        		'title'=>'编辑'
        	);
        	$this->load->view('admin/Catalog_form',$data);
        }else{
            $data=$this->input->post();        //拿到POST过来的数据，塞进数组里面。
            $this->Catalog_model->edit_one($id,$data);
            redirect('admin/catalog_ctrl/index');
        }
	}

	public function del_one($id){
		$this->load->model('Catalog_model');
		$this->Catalog_model->getOne($id);
		$this->Catalog_model->del_one($id);
		redirect('admin/Catalog_ctrl/index');
	}
} 	